package com.main.rentsystem.controller.admin;

import com.main.rentsystem.pojo.dto.UserPageQueryDTO;
import com.main.rentsystem.pojo.entity.PageBean;
import com.main.rentsystem.pojo.entity.Result;
import com.main.rentsystem.pojo.entity.User;
import com.main.rentsystem.pojo.vo.MessageVO;
import com.main.rentsystem.service.UserServer;
import com.main.rentsystem.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController("adminUserController")
@Slf4j
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserServer userServer;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result adminLogin(User user){
        log.info("管理员登录：{}",user);
        if(redisTemplate.opsForValue().get("abc") == null){
            redisTemplate.opsForValue().set("abc","123");
        }
        String password = (String) redisTemplate.opsForValue().get(user.getUsername());
        if(password != null && password.equals(user.getPassword())){
            Map<String , Object> claims = new HashMap<>();
            claims.put("id", "Admin");
            claims.put("username",user.getUsername());
            claims.put("password",user.getPassword());
            //使用JWT工具类，生成身份令牌
            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        }
        return Result.error("fail");
    }

    @GetMapping("/getUserInfo")
    public PageBean getUserInfo(UserPageQueryDTO userPageQueryDTO){
        log.info("getUserInfo");
        return userServer.getUserInfo(userPageQueryDTO);
    }

    @DeleteMapping("/ids")
    public Result deleteById(@RequestParam List<Long> ids){
        log.info("删除用户的编号：{}",ids);
        userServer.deleteBatch(ids);
        return Result.success();
    }

    @PostMapping("/update")
    public Result updateUserInfo(@RequestBody User user){
        log.info("修改用户信息：{}", user);
        userServer.updateById(user);
        return Result.success();
    }

    @GetMapping("/getMessage")
    public Result GetMessage(){
        log.info("管理员查询留言");
        User user = new User();
        List<MessageVO> messageVOList = userServer.GetMessage(user);
        return Result.success(messageVOList);
    }



}
